﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AvatarHelper;
using Avatar.Model;

public partial class Management_ArticleCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                GridView1.DataSource = _adc.NewsCategory.ToList();
                GridView1.DataBind();
            }
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        using (var _adc = DBHelper.GetDataContext())
        {
            if (!string.IsNullOrEmpty(txt_Category.Text))
            {
                NewsCategory _c = new NewsCategory();
                _c.Name = txt_Category.Text;
                _c.Default = 0;

                _adc.NewsCategory.InsertOnSubmit(_c);
                _adc.SubmitChanges();

                txt_Category.Text = "";

                panel_Add.Visible = false;
                btn_AddSelect.Visible = true;

                GridView1.DataSource = _adc.NewsCategory.ToList();
                GridView1.DataBind();
            }
        }
    }
    protected void btn_Edit_Click(object sender, EventArgs e)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _btn = sender as Button;
                var _id = Convert.ToInt32(_btn.CommandArgument);
                var _newsCategory = _adc.NewsCategory.FirstOrDefault(c => c.ID == _id);
                if (_newsCategory == null) throw new Exception("分类不存在或以删除，请刷新后重试。");

                panel_Edit.Visible = true;

                txt_CName.Text = _newsCategory.Name;
                txt_CID.Value = _newsCategory.ID.ToString();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "出错:" + _e.Message;
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _btn = sender as Button;
                var _id = Convert.ToInt32(_btn.CommandArgument);
                var _newsCategory = _adc.NewsCategory.FirstOrDefault(c => c.ID == _id);
                if (_newsCategory != null)
                {
                    if (_newsCategory.News.Count() > 0) throw new Exception("此分类下包含文章若干，不能删除。");

                    _adc.NewsCategory.DeleteOnSubmit(_newsCategory);
                    _adc.SubmitChanges();
                    Response.Redirect(Request.Url.ToString());
                }
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "出错:" + _e.Message;
        }
    }
    protected void btn_AddSelect_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = true;
        btn_AddSelect.Visible = false;
    }
    protected void btn_AddCancel_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = false;
        btn_AddSelect.Visible = true;
    }
    protected void btn_EditSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txt_CName.Text)) throw new Exception("分类名称不能为空");
            if (txt_CName.Text.Length > 10) throw new Exception("分类名称不能超过10个字符");

            using (var _adc = DBHelper.GetDataContext())
            {
                var _id = Convert.ToInt32(txt_CID.Value);
                var _category = _adc.NewsCategory.FirstOrDefault(c=>c.ID==_id);
                if (_category != null)
                {
                    _category.Name = txt_CName.Text;
                    var _default = Convert.ToInt32(ddl_Default.SelectedValue);
                    var _oldCate = _adc.NewsCategory.FirstOrDefault(c=>c.Default==_default);
                    if (_oldCate != null) _oldCate.Default = 0;
                    _category.Default = _default;

                    _adc.SubmitChanges();
                }
                Response.Redirect(Request.Url.ToString());
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "出错:" + _e.Message;
        }
    }
    protected void btn_EditCancel_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
    }
}
